#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
  pid_t pid;
  pid=fork();
  if(pid<0)
  {
    printf("%s fork error\n",__FUNCTION__);
    return 1;
  }else if(pid==0)
  {
    printf("child is run,pid is %d\n",getpid());
    sleep(5);
    exit(1);
  }
  else{
    pid_t ret;
    pid_t status=0;
  while(1){
        ret = waitpid(pid, &status, WNOHANG); 
        if(ret == 0){
            //子进程没有退出，但是waitpid等待是成功的，需要父进程重复进行等待
            printf("Do father things!\n");
        }
        else if(ret > 0){
            //子进程退出了，waitpid也成功了，获取到了对应的结果
            printf("fahter wait: %d, success, status exit code: %d, status exit signal: %d\n", ret, (status>>8)&0xFF, status&0x7F);
            break;
        }
        else{ //ret < 0
            //等待失败
            perror("waitpid");
            break;
        }
        sleep(1);
    }
  }
  return 0;
}
